package com.tmc.web.controller.inthotel;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.tmc.bean.common.Pagination;
import com.tmc.bean.inthotel.hotel.City;
import com.tmc.bean.inthotel.hotel.Country;
import com.tmc.bean.inthotel.hotel.Hotel;
import com.tmc.vendor.inthotel.tourico.service.CityService;
import com.tmc.vendor.inthotel.tourico.service.CountryService;
import com.tmc.vendor.inthotel.tourico.service.HotelService;

@Controller
@RequestMapping("/product")
public class ProductController {
	
	private Log logger = LogFactory.getLog(this.getClass());
	@Autowired
	private CountryService countryService;
	@Autowired
	private CityService cityService;
	@Autowired
	private HotelService hotelService;
	
	@RequestMapping(value="/manager")
	public ModelAndView manager(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		return new ModelAndView("views/product/product_manager", map);
	}
	
	@RequestMapping(value="/country")
	public ModelAndView searchCountry(Country country,Integer pageIndex, Integer pageSize,HttpServletRequest request){
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<Country> page = countryService.findByCon1(country, pageIndex, pageSize);
		map.put("page", page);
		map.put("destination", country);
		return new ModelAndView("views/product/product_manager",map);
	}
	
	@RequestMapping(value="/city")
	public ModelAndView searchCity(City city,Integer pageIndex, Integer pageSize,HttpServletRequest request){
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<City> page = cityService.findByCon(city, pageIndex, pageSize);
		
		map.put("page",page);
		map.put("destination",city);
		return new ModelAndView("views/product/product_manager",map);
	}
	
	@RequestMapping(value="/hotel")
	public ModelAndView searchHotel(Hotel hotel,Integer pageIndex, Integer pageSize,HttpServletRequest request){
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<Hotel> page =  hotelService.findByCon(hotel, pageIndex, pageSize);
		
		map.put("page", page);
		map.put("destination",hotel);
		return new ModelAndView("views/product/product_manager",map);
	}

	@RequestMapping(value="/saveCountry")
	public ModelAndView saveManagerInfo(Country country){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			countryService.updateManagerInfo(country);
			map.put("result","true");
			return new ModelAndView("views/product/product_manager",map);
		}catch(Exception e){
			e.printStackTrace();
			map.put("result","false");
			return new ModelAndView("views/product/product_manager",map);
		}
	}

}
